﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UniSync.Core;
using System.Configuration;

namespace UniSync
{
    class Program
    {

        private static AzureSync mUniSync;

        private static String mLocalConnStr;
        private static String mtAzureConnStr;
        private static String mScopeName;
        private static String[] mTableNameToSync;
        private static int mNbDays = 0;


        private static String LocalConnStr
        {
            get
            {
                if (String.IsNullOrWhiteSpace(mLocalConnStr))
                {
                    mLocalConnStr = ConfigurationManager.ConnectionStrings["LocalConnectionString"].ToString();
                }
                return mLocalConnStr;
            }
            set { mLocalConnStr = value; }
        }

        private static String AzureConnStr
        {
            get
            {
                if (String.IsNullOrWhiteSpace(mtAzureConnStr))
                {
                    mtAzureConnStr = ConfigurationManager.ConnectionStrings["CloudConnectionString"].ToString();
                }
                return mtAzureConnStr;
            }
            set { mtAzureConnStr = value; }
        }

        private static String ScopeName
        {
            get
            {
                if (String.IsNullOrWhiteSpace(mScopeName))
                {
                    mScopeName = ConfigurationManager.AppSettings["SyncScopeName"];
                }
                return mScopeName;
            }
            set { mScopeName = value; }
        }

        private static String[] TableNameToSync
        {
            get
            {
                if (mTableNameToSync == null)
                {
                    mTableNameToSync = ConfigurationManager.AppSettings["TablesToSync"].Split(',');
                }
                return mTableNameToSync;
            }
            set { mTableNameToSync = value; }
        }

        private static int NbDays
        {
            get
            {
                if (mNbDays == 0)
                {
                    mNbDays = Convert.ToInt32(ConfigurationManager.AppSettings["DaysOfRetention"]);
                }
                return mNbDays;
            }
            set { mNbDays = value; }
        }

       
        /// <summary>
        /// UniSync is use to Sychronize A SQL on-premise database with a AzureSQL
        /// Param:
        ///  -setup: Run once this will add the table and the tools needed on both databases. Source IS THE ON-PREMISE
        ///  -reset: Reset the MetaData for the current Scope
        ///  -clean: Delete the metadata for the current Scope, older than the specified number of days in the config file
        ///  -delete: Delete ALL table related to the Sync Framework
        ///  -sync: do the synchronization in both direction
        /// </summary>
        /// <param name="args">Pass the operation to execute (Default:sync)</param>
        [STAThread]
        static void Main(string[] args)
        {
            Init();
            if (args.Length == 0)
            {
                mUniSync.Sync();
            }
            else
            {
                switch (args[0]) { 
                    case("-setup"):
                    case ("setup"):
                        mUniSync.Setup();
                        break;
                    case ("-reset"):
                    case ("reset"):
                        mUniSync.ScopeReset();
                        break;
                    case ("-clean"):
                    case ("clean"):
                        mUniSync.CleanDataScopeOlderThan(NbDays);
                        break;
                    case ("-delete"):
                    case ("delete"):
                        mUniSync.DeleteSync();
                        break;
                    default: 
                        mUniSync.Sync();
                        break;
                }

            }
        }

       private static void Init() {
           mUniSync = new AzureSync(LocalConnStr, AzureConnStr, ScopeName, TableNameToSync);
        }



    }
}
